/*
 * Licensed to Selene developers ('Selene') under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Selene licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "selene.h"
#include "sln_tests.h"
#include "sln_tok.h"
#include <string.h>
#include <stdio.h>
#include "../lib/parser/parser.h"

/**
 * Packet Capture from OpenSSL s_client sending a client hello, no TLS protocol wrapper.
 */
static unsigned char openssl_client_hello_basic[] = {
  0x01, 0x00, 0x00, 
  0xca, 0x03, 0x01, 0x4d, 0xc5, 0xa9, 0x90, 0x4f, 
  0xfe, 0x47, 0x4c, 0xc4, 0x64, 0x34, 0x1b, 0x73, 
  0x8f, 0xb3, 0xd5, 0xbc, 0xc9, 0xde, 0xdf, 0xbc, 
  0xec, 0x76, 0x4d, 0x9e, 0x28, 0x2e, 0x1c, 0xf0, 
  0xf2, 0x60, 0xc7, 0x00, 0x00, 0x5c, 0xc0, 0x14, 
  0xc0, 0x0a, 0x00, 0x39, 0x00, 0x38, 0x00, 0x88, 
  0x00, 0x87, 0xc0, 0x0f, 0xc0, 0x05, 0x00, 0x35, 
  0x00, 0x84, 0xc0, 0x12, 0xc0, 0x08, 0x00, 0x16, 
  0x00, 0x13, 0xc0, 0x0d, 0xc0, 0x03, 0x00, 0x0a, 
  0xc0, 0x13, 0xc0, 0x09, 0x00, 0x33, 0x00, 0x32, 
  0x00, 0x9a, 0x00, 0x99, 0x00, 0x45, 0x00, 0x44, 
  0xc0, 0x0e, 0xc0, 0x04, 0x00, 0x2f, 0x00, 0x96, 
  0x00, 0x41, 0x00, 0x07, 0xc0, 0x11, 0xc0, 0x07, 
  0xc0, 0x0c, 0xc0, 0x02, 0x00, 0x05, 0x00, 0x04, 
  0x00, 0x15, 0x00, 0x12, 0x00, 0x09, 0x00, 0x14, 
  0x00, 0x11, 0x00, 0x08, 0x00, 0x06, 0x00, 0x03, 
  0x00, 0xff, 0x02, 0x01, 0x00, 0x00, 0x44, 0x00, 
  0x0b, 0x00, 0x04, 0x03, 0x00, 0x01, 0x02, 0x00, 
  0x0a, 0x00, 0x34, 0x00, 0x32, 0x00, 0x01, 0x00, 
  0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 
  0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 
  0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, 
  0x0e, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x11, 0x00, 
  0x12, 0x00, 0x13, 0x00, 0x14, 0x00, 0x15, 0x00, 
  0x16, 0x00, 0x17, 0x00, 0x18, 0x00, 0x19, 0x00, 
  0x23, 0x00, 0x00
};

static void handshake_io_client_hello(void **state)
{
  selene_error_t *err;
  sln_parser_baton_t *baton;
  selene_conf_t *conf = NULL;
  selene_t *s = NULL;
  sln_bucket_t *e1;
  size_t maxlen = sizeof(openssl_client_hello_basic);
  size_t i;

  selene_conf_create(&conf);
  SLN_ERR(selene_conf_use_reasonable_defaults(conf));
  SLN_ERR(selene_server_create(conf, &s));
  SLN_ASSERT_CONTEXT(s);

  baton = (sln_parser_baton_t *)s->backend_baton;

  for (i = 1; i <= maxlen; i++) {
    SLN_ERR(sln_bucket_create_copy_bytes(sln_test_alloc, &e1,
                                         (const char*)openssl_client_hello_basic,
                                         i));
    SLN_BRIGADE_INSERT_TAIL(baton->in_handshake, e1);
    err  = sln_io_handshake_read(s, baton);
    if (err) {
      SLN_ASSERT(err->err == SELENE_EINVAL);
      selene_error_clear(err);
    }
    else {
      /* TODO: more asserts */
    }
    sln_brigade_clear(baton->in_handshake);
  }

  selene_destroy(s);
  selene_conf_destroy(conf);
}


/**
 * Packet Capture from curl sending a client hello, no TLS protocol wrapper,
 * includes SNI for 'www.apache.org' as the only extension.
 */
static unsigned char curl_client_hello_sni[] = {
  0x01, 0x00, 0x00,
  0x6d, 0x03, 0x01, 0x4d, 0xfd, 0x06, 0x2f, 0x6b,
  0x25, 0xa2, 0x88, 0xee, 0xfc, 0x8d, 0x18, 0xc5,
  0xf6, 0x7d, 0x64, 0x7f, 0xa7, 0x3c, 0xf3, 0x53,
  0x04, 0x15, 0x6f, 0x24, 0x0a, 0x7c, 0x39, 0x2a,
  0x02, 0x59, 0x22, 0x00, 0x00, 0x2c, 0x00, 0x39,
  0x00, 0x38, 0x00, 0x35, 0x00, 0x16, 0x00, 0x13,
  0x00, 0x0a, 0x00, 0x33, 0x00, 0x32, 0x00, 0x2f,
  0x00, 0x9a, 0x00, 0x99, 0x00, 0x96, 0x00, 0x05,
  0x00, 0x04, 0x00, 0x15, 0x00, 0x12, 0x00, 0x09,
  0x00, 0x14, 0x00, 0x11, 0x00, 0x08, 0x00, 0x06,
  0x00, 0x03, 0x02, 0x01, 0x00, 0x00, 0x17, 0x00,
  0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e,
  0x77, 0x77, 0x77, 0x2e, 0x61, 0x70, 0x61, 0x63,
  0x68, 0x65, 0x2e, 0x6f, 0x72, 0x67
};

static void handshake_io_client_hello_sni(void **state)
{
  selene_error_t *err;
  sln_parser_baton_t *baton;
  selene_conf_t *conf = NULL;
  selene_t *s = NULL;
  sln_bucket_t *e1;
  size_t maxlen = sizeof(curl_client_hello_sni);
  size_t i;

  selene_conf_create(&conf);
  SLN_ERR(selene_conf_use_reasonable_defaults(conf));
  SLN_ERR(selene_server_create(conf, &s));
  SLN_ASSERT_CONTEXT(s);

  baton = (sln_parser_baton_t *)s->backend_baton;

  for (i = 1; i <= maxlen; i++) {
    SLN_ERR(sln_bucket_create_copy_bytes(sln_test_alloc, &e1,
                                         (const char*)curl_client_hello_sni,
                                         i));
    SLN_BRIGADE_INSERT_TAIL(baton->in_handshake, e1);
    err  = sln_io_handshake_read(s, baton);
    if (err) {
      SLN_ASSERT(err->err == SELENE_EINVAL);
      selene_error_clear(err);
    }
    else {
      /* TODO: more asserts */
    }
    sln_brigade_clear(baton->in_handshake);
  }

  selene_destroy(s);
  selene_conf_destroy(conf);
}

/**
 * Packet Capture from apache 2.3 sending a server hello, no TLS protocol wrapper,
 * RC4-SHA1 as the selected cipher, and SNI as the only extension.
 */
static unsigned char apache_server_hello_sni[] = {
  /* content-type server hello */
  0x02,
  /* length  of hs message */
  0x00, 0x00, 0x4c,
  /* TLS version */
  0x03, 0x01,
  /* Random */
  0x4d, 0xfd, 0x06, 0x2f, 0x6f,
  0x42, 0xf9, 0x7d, 0xee, 0x2e, 0x3a, 0xbc, 0xce,
  0xc1, 0x6a, 0x34, 0xe6, 0xa1, 0xde, 0xec, 0x47,
  0x84, 0xa9, 0xf3, 0x5f, 0xae, 0x99, 0x02, 0xfb,
  0x34, 0x1a, 0x0a,
  /* session ID length */
  0x20,
  /* session id value */
  0xdd, 0x0e, 0x54, 0x37,
  0x37, 0x97, 0x5a, 0x0f, 0x81, 0xe1, 0x2b, 0x7c,
  0xbe, 0x43, 0x8b, 0x9f, 0x4d, 0xcc, 0xf4, 0x26,
  0xf5, 0x85, 0x3d, 0x60, 0xc7, 0xff, 0xd8, 0xb8,
  0x2e, 0x71, 0x83, 0x4c,
  /* cipher suite*/
  0x00, 0x05,
  /* compression method */
  0x01,
  /* extensions length (total) */
  0x00, 0x04,
  /* SNI extension (0) */
  0x00, 0x00,
  /* sni extension length (0), nothing after it */
  0x00, 0x00
};

static void handshake_io_server_hello_sni(void **state)
{
  selene_error_t *err;
  sln_parser_baton_t *baton;
  selene_conf_t *conf = NULL;
  selene_t *s = NULL;
  sln_bucket_t *e1;
  size_t maxlen = sizeof(apache_server_hello_sni);
  size_t i;

  selene_conf_create(&conf);
  SLN_ERR(selene_conf_use_reasonable_defaults(conf));
  SLN_ERR(selene_client_create(conf, &s));
  SLN_ASSERT_CONTEXT(s);

  baton = (sln_parser_baton_t *)s->backend_baton;

  for (i = maxlen; i <= maxlen; i++) {
    SLN_ERR(sln_bucket_create_copy_bytes(sln_test_alloc, &e1,
                                         (const char*)apache_server_hello_sni,
                                         i));
    SLN_BRIGADE_INSERT_TAIL(baton->in_handshake, e1);
    err  = sln_io_handshake_read(s, baton);
    if (err) {
      SLN_ASSERT(err->err == SELENE_EINVAL);
      selene_error_clear(err);
    }
    else {
      /* TODO: more asserts */
    }
    sln_brigade_clear(baton->in_handshake);
  }

  selene_destroy(s);
  selene_conf_destroy(conf);
}


SLN_TESTS_START(handshake_io)
  SLN_TESTS_ENTRY(handshake_io_client_hello)
  SLN_TESTS_ENTRY(handshake_io_client_hello_sni)
  SLN_TESTS_ENTRY(handshake_io_server_hello_sni)
SLN_TESTS_END()
